interface typeParams {
  type: string | number;
}

class Notification {
  events = new Map(); // 存储事件
  // maxListeners = 9999; // 最大监听数量
  constructor() {}
  // 监听通知
  addObserver(type: typeParams, fn: void): void {
    const handler = this.events.get(type); // 获取对应事件名称的函数清单
    if (handler) {
      handler.push(fn);
    } else {
      this.events.set(type, [fn]);
    }
  }
  // 发送通知
  postNotification(type: typeParams, ...args: any): void {
    let handler = this.events.get(type);
    console.log(handler, '--handler--');
    if (!handler) return;
    for (let item of handler) {
      if (args.length > 0) {
        item.apply(this, args);
      } else {
        item.call(this);
      }
    }
  }
  // 移除通知
  removeObserver(type: typeParams): void {
    const handler = this.events.get(type); // 获取对应事件名称的函数清单
    if (handler) this.events.delete(type);
  }
  // 移除所有通知
  removeObserverAll(): void {
    this.events = new Map();
  }
}

const emitter = new Notification();

export default emitter;
